$(function () {
    const emptyMessage = "没有未读通知!";
    const notice = $('#notifications');  // 便于对class的样式进行修改

    function CheckNotifications() {
        $.ajax({
            url:'/notifications/latest-notifications/',
            cache:false,
            success: function (data) {
                if (!data.includes(emptyMessage)) {
                    // 如果data为空
                    notice.addClass('btn-danger');
                }
            }
        })
    }

    CheckNotifications();   // 页面加载时执行

    function update_social_activity(id_value) {
        // 动态的唯一值
        const newsToUpdate = $('[news-id=' + id_value + ']');
        $.ajax({
            url:'/news/update-interactions/',
            data:{'id_value':id_value},
            type:"POST",
            cache:false,
            success: function (data) {
                // 实时更新点赞数,评论数
                $(".like-count",newsToUpdate).text(data.likes);
                $(".comment-count",newsToUpdate).text(data.comments);

            },
        });
    }
    
    notice.click(function () {
        if ($('.popover').is(':visible')){
            // 如果通知栏下拉状态
            notice.popover('hide');
            CheckNotifications();
        }else{
            // 如果通知栏没有下拉
            notice.popover('dispose');
            $.ajax({
                url:'/notifications/latest-notifications/',
                cache:false,
                success: function (data) {
                    notice.popover({
                        html:true,
                        trigger:'focus',
                        container:'body',
                        placement:'bottom',
                        content:data,
                    });
                    notice.popover('show');
                    notice.removeClass('btn-danger');
                }
            });
        }
        return false;
    });

    // WebSocket连接,使用wss(https)或者ws(http)
    const ws_scheme = window.location.protocol === 'https:' ? 'wss' :'ws';
    const ws_path = ws_scheme + '://' + window.location.host + '/ws/notifications/';
    const ws = new ReconnectingWebSocket(ws_path);

    ws.onmessage = function (event) {
        console.log(event.data);
        const data = JSON.parse(event.data);
        switch(data.key) {
            case "notification":
                if (currentUser !== data.actor_name){
                    // 消息提示的发起者不提示
                    notice.addClass('btn-danger');
                }
                break;

            case "social_update":
                 if (currentUser !== data.actor_name){
                    // 消息提示的发起者不提示
                    notice.addClass('btn-danger');
                }
                 // 更新点赞数和评论数
                update_social_activity(data.id_value);
                break;

            case "additional_news":
                 if (currentUser !== data.actor_name){
                    // 消息提示的发起者不提示
                    $('.stream-update').show();  // 显示新的动态
                }
                break;

            default:
                console.log('error', data);
                break;
        }
    };
});